﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace Project
{
    public partial class rent : Form
    {
        string bookid;
        string studentId;
        string name;
        string oldLoanid;
        private OleDbConnection bookConn;
        private OleDbCommand oleDbCmd = new OleDbCommand();

        private String connParam = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Properties.Settings.Default.database + ";Persist Security Info=False";



        public rent(string Bookid, string StudentId, string Name, string iid)
        {
            bookid = Bookid;
            studentId = StudentId;
            name = Name;
            oldLoanid = iid;
            bookConn = new OleDbConnection(connParam); 
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
                DateTime time = DateTime.Now;
                string dateFormat = "dd/MM/yyyy";
                DateTime returnDate = DateTime.Now;
            
                if ((txtReturn.Text == "") || (Convert.ToInt32(txtReturn.Text) < 0))
                {
                    if (txtReturn.Text == "")
                        MessageBox.Show("Please enter number of days for rent OR Select date from calender.");
                    else if (Convert.ToInt32(txtReturn.Text) < 0)
                        MessageBox.Show("Selected date is before current date.");
                }
                else
                {
                    returnDate = returnDate.AddDays(Convert.ToUInt16(txtReturn.Text));
                    bookConn.Open();
                    oleDbCmd.Connection = bookConn;

                    oleDbCmd.CommandText = "insert into loan (sID,iID,start_date,end_date,status, iName) values('" + studentId + "', '" + bookid + "', '" + time.ToString(dateFormat) + "', '" + returnDate.ToString(dateFormat) + "', 'rented', '" + name + "') ;";
                    try
                    {
                        int temp = oleDbCmd.ExecuteNonQuery();
                        if (temp > 0)
                        {
                            if (oldLoanid != "none")
                            {
                                oleDbCmd.CommandText = "UPDATE loan SET hold = '' where ID = " + oldLoanid + " ;";
                                oleDbCmd.ExecuteNonQuery();
                            }
                            DialogResult dialogResult = MessageBox.Show("Loan added successfully.\n\nLoan another item?", "", MessageBoxButtons.YesNo);
                            if (dialogResult == DialogResult.Yes)
                            {
                                //do something
                            }
                            else if (dialogResult == DialogResult.No)
                            {
                                bookConn.Close();
                                this.Close();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Record Fail to Added");
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Record Fail to Added");
                    }

                    bookConn.Close();

                }
            }
          

        

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void rent_Load(object sender, EventArgs e)
        {

        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            DateTime time = DateTime.Now;
            // Use current time
              DateTime returnDate = DateTime.Now;
            returnDate = returnDate.AddDays(17);

            DateTime time1 = monthCalendar1.SelectionRange.Start;
            TimeSpan span = time1 - time;
            int diffDays = (time1.Date - time.Date).Days;
            txtReturn.Text = diffDays.ToString();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}
